
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ScaleWidget extends StatefulWidget {

  Widget child;
  double? scale;
  int? milliseconds;

  ScaleWidget(this.child, {this.scale, this.milliseconds, Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _ScaleState();
}

class _ScaleState extends State<ScaleWidget> with SingleTickerProviderStateMixin {

  late AnimationController controller;
  late Animation<double> animation;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(vsync: this, duration: Duration(milliseconds: widget.milliseconds ?? 600));
    animation = Tween(begin: 1.0, end: widget.scale ?? 0.88).animate(controller)
      ..addListener(() {
        setState(() {});
      })
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          controller.reverse();
        } else if (status == AnimationStatus.dismissed) {
          controller.forward();
        }
      });

    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return ScaleTransition(
      scale: animation,
      child: widget.child,
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}